package com.xudow.app.newui.task;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;

import com.activeshare.edu.ucenter.common.messages.user.UserDetailMessage;
import com.google.gson.Gson;
import com.xudow.app.Config;
import com.xudow.app.XApplication;
import com.xudow.app.network.AjaxCallBack;
import com.xudow.app.network.OkHttpClientManager;
import com.xudow.app.util.rest.XRestTemplate;

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

import static com.xudow.app.util.LogUtils.LOGE;
import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 作者：LownChen on 2016/10/24 16:09
 */
public class UserProfileTaskls {


    private static final String USER_PROFILE_URL = Config.BASE_URL + Config.USER_PROFILE_URL;

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;

    private Context context;
    private Handler handler;

    public UserProfileTaskls(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    private void sendErrorMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = ERROR;

        if (null != msg) {
            Bundle data = new Bundle();
            data.putString("errMessage", msg);
            message.setData(data);
        }

        handler.sendMessage(message);
    }

    private void sendSuccessMessage(UserDetailMessage profile) {
        android.os.Message message = new android.os.Message();
        message.what = SUCCESS;

        Bundle data = new Bundle();
        data.putSerializable("profile", profile);
        message.setData(data);

        handler.sendMessage(message);
    }


    public void up() {


        OkHttpClientManager okHttpClientManager = new OkHttpClientManager();
        Map<String, String> map = new HashMap<>();
        okHttpClientManager.getAsyn(context, USER_PROFILE_URL, map, new AjaxCallBack() {
            @Override
            public void onSucces(String response) throws Exception {
                try {
                    UserDetailMessage result = new Gson().fromJson(USER_PROFILE_URL,
                            UserDetailMessage.class);
            /*String result1=restTemplate1.getForObject(USER_PROFILE_URL,
                    String.class);*/
                    if (result != null) {
                        //FileUtils.writeFile(result1,new File(context.getCacheDir().getAbsolutePath(),"userfile.txt"));
                        if (result.getResult().equals("0")) {
                            sendSuccessMessage(result);
                        } else if (result.getResult().equals("1")) {
                            sendErrorMessage(result.getErrorMessage());
                        }
                    } else {
                        sendErrorMessage(null);
                    }
                } catch (Exception e) {


                    sendErrorMessage(e.getMessage());
                }

            }

            @Override
            public void onFail(String response) {

            }
        });


    }

}
